package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    //分页and查询名字
    @GetMapping("/setmeal/page")
    public ResultInfo findByPageAndName(@RequestParam(value = "page",defaultValue = "1") Integer pageNum,
                                        @RequestParam(defaultValue = "10") Integer pageSize,
                                        String name){
        Page<Setmeal> pageResult = setmealService.findByPageAndName(pageNum,pageSize,name);
        return ResultInfo.success(pageResult);
    }

    @PostMapping("/setmeal")
    public ResultInfo add(@RequestBody Setmeal setmeal){
        Boolean flag = setmealService.add(setmeal);
        return flag ? ResultInfo.success(null) : ResultInfo.error("套餐添加失败");
    }

    //批量启售停售
    @PostMapping("/setmeal/status/{status}")
    public ResultInfo statusConvert(@PathVariable Integer status,
                                    @RequestParam List<Long> ids){
        System.out.println(status);
        System.out.println(ids);
        setmealService.statusConvert(status, ids);
        return ResultInfo.success(null);
    }

    @DeleteMapping("/setmeal")
    public ResultInfo delete(@RequestParam List<Long> ids){
        Boolean flag = setmealService.delete(ids);
        return flag ? ResultInfo.success(null) : ResultInfo.error("不能删除启售套餐");
    }

    //修改回显
    @GetMapping("/setmeal/{id}")
    public ResultInfo findById(@PathVariable Long id){
        Setmeal setmeal = setmealService.findById(id);
        return ResultInfo.success(setmeal);
    }

    //修改
    @PutMapping("/setmeal")
    public ResultInfo updateSetMeal(@RequestBody Setmeal setmeal){
        setmealService.update(setmeal);
        return ResultInfo.success(null);
    }
}
